package com.mkyong.soa.server;

import java.util.Date;

import org.apache.mina.core.service.IoHandlerAdapter;
import org.apache.mina.core.session.IdleStatus;
import org.apache.mina.core.session.IoSession;

public class TimeServerHandler extends IoHandlerAdapter {
	@Override
	public void exceptionCaught(IoSession session, Throwable cause)
			throws Exception {
		cause.printStackTrace();
	}
	
	
	@Override
	public void messageReceived(IoSession iosession, Object obj)
			throws Exception {
		String msg = obj.toString();
		if(msg.trim().equalsIgnoreCase("quit")){
			iosession.close();
			return;
		}
		
		Date date = new Date();
		iosession.write(date.toString());	
		System.out.println("msg:"+msg);
		System.out.println("msg writing");
		
	}
	
	@Override
	public void sessionIdle(IoSession iosession, IdleStatus status)
			throws Exception {
		System.out.println("IDLE "+iosession.getIdleCount(status));
	}
}
